<template>
	<div>
		<navigation-bar>
			<div class="searchContent">
				<el-input v-model="name" placeholder="请输入名称" size="mini"
					style="width: 150px; margin-right: 10px"></el-input>
				<custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
				<custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
				<!--				<custom-button-->
				<!--					type="print"-->
				<!--					style="margin-right: 10px"-->
				<!--					@printClick="printClick"-->
				<!--				></custom-button>-->
				<custom-button type="add" @addClick="addClick"></custom-button>
			</div>
		</navigation-bar>
		<el-row>
			<el-col :span="5">
				<div style="width: 100%">
					<user-list @handleSelectionChange="handleSelectionChange" stage="ing" businessType="bloodCollect"
						ref="userList" @addClick="addClick"></user-list>
				</div>
			</el-col>
			<el-col :span="19">
				<table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" :pageSize="pageSize"
					:pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @editRow="editRow" @viewRow="viewRow"
					@deleteRow="deleteRow"></table-index>
			</el-col>
		</el-row>
		<div v-if="dialogShow">
			<custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
				<form-index ref="form" :formConfig="formConfig" @submit="submit" :type="type"
					@formSelectChange="formSelectChange">
					<!--  <div>这里还可以插入其它内容进行拓展</div> -->
				</form-index>
			</custom-dialog>
		</div>
	</div>
</template>

<script>
import { bloodSampleCollectionRecordForm } from "@/assets/form/managementCenter/subjectManagement/testPeriodManagement/bloodSampleCollectionRecordForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import userList from "@/components/customComponents/managementCenter/userList"
import { AjaxCommon } from "@/libs/apiCommon"
import { PopupCommon } from "@/libs/popupCommon"
import { SessionCommon } from "@/libs/sessionCommon"
export default {
	components: {
		navigationBar,
		customButton,
		tableIndex,
		customDialog,
		formIndex,
		userList,
	},
	data() {
		return {
			collectType: [],
			samplePoint: [],
			leftSelectIds: [],
			leftDataSource: [],
			leftTableColumns: [
				{
					prop: "examineeName",
					label: "受试者姓名",
				},
			],
			testIndex: "",
			name: "",
			signPrefix: "",
			// table 相关参数  begin
			dataSource: [],
			tableColumns: [
				{
					prop: "randomNo",
					label: "随机号",
					width: 80,
				},
				{
					prop: "groupSeq",
					label: "入组顺序号",
					width: 100,
				},
				{
					prop: "examineeName",
					label: "受试者姓名",
					width: 100,
				},
				{
					prop: "sex",
					label: "性别",
					dictName: "sex",
					width: 60,
				},
				{
					prop: "sampleType",
					label: "样品类型",
					dictName: "sample_type",
					width: 80,
				},
				{
					prop: "sampleSeq",
					label: "采样序号",
					width: 80,
				},
				{
					prop: "samplePoint",
					label: "采样时间点",
					width: 100,
				},
				{
					prop: "sampleBeginTime",
					label: "采样开始时间",
					width: 160,
				},
				{
					prop: "sampleEndTime",
					label: "采样结束时间",
					width: 160,
				},
				{
					prop: "planBeginTime",
					label: "计划开始时间",
					width: 160,
				},
				{
					prop: "planEndTime",
					label: "计划结束时间",
					width: 160,
				},
				{
					prop: "createByName",
					label: "采样人",
					width: 100,
				},
				{
					prop: "createTime",
					label: "记录时间",
					width: 160,
				},
				{
					prop: "verifyByName",
					label: "核对人",
					width: 100,
				},
				{
					prop: "verifyTime",
					label: "核对时间",
					width: 160,
				},
				{
					prop: "isUltraWindows",
					label: "是否超窗",
					dictName: "yn",
					width: 80,
				},
				{
					prop: "collectionCondition",
					label: "采样条件",
					width: 300,
				},
				{
					prop: "remarks",
					label: "异常备注",
					width: 300,
				},
			],
			btnList: ["edit", "view", "delete"],
			pageSize: 20,
			pageCount: 100,
			pageNo: 1,
			// table 相关参数  end

			// 弹窗是否显示
			dialogShow: false,
			// 弹窗标题 (新增修改不同)
			dialogTitle: "",
			// form的配置信息
			formConfig: [],
			type: "add",
		}
	},
	methods: {
		formSelectChange(e) {
			if (e.filedName == "sampleType") {
				let self = this
				var data = self.collectType.find((t) => t.type == e.value)
				console.log(data, "data......")
				var sampleType = data.type
				self.testIndex = data.testIndex
				var examineeCode = e.allData.examineeCode
				AjaxCommon(
					"/ccdc/pm/em/ing/emIngBloodSample/getOptionSamplePoint",
					"post",
					{
						examineeCode: examineeCode,
						sampleType: sampleType,
						testIndex: self.testIndex,
					}
				)
					.then((res) => {
						if (res.data.code == 200) {
							var samplePoint = res.data.result?.map((t) => {
								t.label = "基于给药序号[" + t.drugNo + "]\xa0\xa0\xa0\xa0\xa0\xa0" + "|" + "\xa0\xa0\xa0\xa0\xa0\xa0采样点[" + t.samplePoint + "]"
								t.value = t.samplePoint
								return t
							})
							self.samplePoint = samplePoint
							self.$refs.form.changeSelectItemList(samplePoint, "samplePoint")
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			}
			if (e.filedName == 'samplePoint') {
				let item = this.samplePoint.find((c) => c.value == e.value)
				const formValue = this.$refs.form.getFormValue()
				formValue.planBeginTime = item.planBeginTime
				formValue.planEndTime = item.planEndTime
				formValue.collectionCondition = item.collectionCondition
				this.$refs.form.changeFormValue(formValue)
			}
		},
		/* 获取样品类别 */
		getCollectType() {
			let self = this
			AjaxCommon(
				"/ccdc/cfg/exp/root/expSample/getOptionByCollectType",
				"post",
				{
					collectType: "blood",
				}
			)
				.then((res) => {
					if (res.data.code == 200) {
						var sampleDataList = res.data.result?.map((t) => {
							t.label = t.testIndex + "(" + t.typeName + ")"
							t.value = t.type
							return t
						})
						self.collectType = sampleDataList
						self.formConfig = bloodSampleCollectionRecordForm(sampleDataList)
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
				.catch((err) => {
					PopupCommon.notify("error", err.message)
				})
		},
		submit(d) {
			let self = this
			if (d.id) {
				AjaxCommon("/ccdc/pm/em/ing/emIngBloodSample/edit", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							this.$refs.userList.refresh()
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			} else {
				d.examineeCode = self.leftSelectIds[0].examineeCode
				d.testIndex = self.testIndex
				d.examineeEn = self.leftSelectIds[0].examineeEn
				d.examineeName = self.leftSelectIds[0].examineeName
				d.filterNo = self.leftSelectIds[0].filterNo
				d.groupSeq = self.leftSelectIds[0].groupSeq
				d.randomNo = self.leftSelectIds[0].randomNo
				d.drugSeq = self.leftSelectIds[0].drugSeq
				d.sex = self.leftSelectIds[0].sex
				delete d.id
				if (d.samplePoint) {
					var newData = self.samplePoint.find(
						(t) => t.samplePoint == d.samplePoint
					)
					d.ttwValue = newData.ttwValue
					d.planBeginTime = newData.planBeginTime
					d.planEndTime = newData.planEndTime
					d.sampleSeq = newData.sampleSeq
					d.drugNo = newData.drugNo
					d.samplePoint = newData.samplePoint
					d.reCalcUltraWindows = newData.reCalcUltraWindows
				}
				console.log(d, "d....")
				AjaxCommon("/ccdc/pm/em/ing/emIngBloodSample/add", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							this.$refs.userList.refresh()
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			}
		},
		dialogClose() {
			this.dialogShow = false
		},
		searchClick() {
			this.pageNo = 1
			this.initPage()
		},
		resetClick() {
			this.name = ""
			this.signPrefix = ""
		},
		addClick() {
			if (this.leftSelectIds.length != 1) {
				PopupCommon.notify("info", "请勾选一个受试者")
				return
			}
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						if (t[res] == "examineeCode") {
							t.filedValue = this.leftSelectIds[0].examineeCode
						} else if (t[res] == "examineeName") {
							t.filedValue = this.leftSelectIds[0].examineeName
						} else if (t[res] == "randomNo") {
							t.filedValue = this.leftSelectIds[0].randomNo
						} else if (t[res] == "groupSeq") {
							t.filedValue = this.leftSelectIds[0].groupSeq
						} else {
							t.filedValue = ""
						}
					}
				})
			})
			this.formConfig = formConfigItem

			this.type = "add"
			this.dialogShow = true
			this.dialogTitle =
				"新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		printClick() {
			if (this.leftSelectIds.length != 1) {
				PopupCommon.notify("info", "请勾选一个受试者")
				return
			}
			PopupCommon.notify("info", "调用打印接口")
		},
		// 分页点击事件
		pageChange(p) {
			this.pageNo = p
			this.initPage()
		},
		editRow(d) {
			console.log(d.data.sampleType, d.data.samplePoint)
			if (d.data.sampleType && d.data.samplePoint) {
				let self = this
				var data = self.collectType.find((t) => t.type == d.data.sampleType)
				var sampleType = data.type
				var testIndex = data.testIndex
				var examineeCode = d.data.examineeCode
				AjaxCommon(
					"/ccdc/pm/em/ing/emIngBloodSample/getOptionSamplePoint",
					"post",
					{
						examineeCode: examineeCode,
						sampleType: sampleType,
						testIndex: testIndex,
					}
				)
					.then((res) => {
						if (res.data.code == 200) {
							var samplePoint = res.data.result?.map((t) => {
								t.label = "基于给药序号[" + t.drugNo + "]\xa0\xa0\xa0\xa0\xa0\xa0" + "|" + "\xa0\xa0\xa0\xa0\xa0\xa0采样点[" + t.samplePoint + "]"
								t.value = t.samplePoint
								return t
							})
							self.samplePoint = samplePoint
							self.$refs.form.changeSelectItemList(samplePoint, "samplePoint")
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			}
			this.type = "edit"
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						if (t[res] == "sampleTime") {
							t.filedValue = [d.data.sampleBeginTime, d.data.sampleEndTime]
						} else {
							t.filedValue = d.data[t[res]]
						}
					}
				})
			})
			this.formConfig = formConfigItem
			this.dialogShow = true
			this.dialogTitle =
				"编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		viewRow(d) {
			this.type = "look"
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						if (t[res] == "sampleTime") {
							t.filedValue = [d.data.sampleBeginTime, d.data.sampleEndTime]
						} else {
							t.filedValue = d.data[t[res]]
						}
					}
				})
			})
			this.formConfig = formConfigItem
			this.dialogShow = true
			this.dialogTitle =
				"查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		deleteRow(d) {
			const id = d.data.id
			let self = this
			AjaxCommon(
				"/ccdc/pm/em/ing/emIngBloodSample/delete?id=" + id,
				"delete",
				{ optReason: d.optReason }
			).then((res) => {
				console.log(res, "res...............")
				if (res.data.code == 200) {
					PopupCommon.notify("success", "删除成功")
					this.$refs.userList.refresh()
					self.initPage()
					self.dialogClose()
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
		// 左侧选中事件
		handleSelectionChange(d) {
			this.leftSelectIds = d
			console.log(d)
			this.examineeCodes = d ? d.map((t) => t.examineeCode) : []
			this.initPage()
		},
		// 初始化事件
		initPage() {
			let self = this
			AjaxCommon("/ccdc/pm/em/ing/emIngBloodSample/listByPage", "post", {
				examineeName: this.name,
				pageNo: this.pageNo,
				pageSize: this.pageSize,
				examineeCodeList: this.examineeCodes,
			}).then((res) => {
				if (res.data.code == 200) {
					self.dataSource = res.data.result.records
					self.pageCount = res.data.result.total
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
	},
	mounted() {
		this.getCollectType()
		this.initPage()
	},
}
</script>

<style></style>
